var config = require('../config.js');
function formatTime(date) {
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();
    return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':');
}
function formatDate(strTime) {
    var date = new Date(strTime);
    return date.getFullYear() + '年' + (date.getMonth() + 1) + '月' + date.getDate() + '日';
}
function formatNumber(n) {
    n = n.toString();
    return n[1] ? n : '0' + n;
}
function accMul(a, b) {
    var c = 0;
    var d = a.toString();
    var e = b.toString();
    if (d.toString().split('.').length>1) {
      try {
          c += d.split('.')[1].length;
      } catch (f) {
          console.log('CatchClause', f);
          console.log('CatchClause', f);
      }
    }
    if (e.toString().split('.').length>1) {
      try {
          c += e.split('.')[1].length;
      } catch (f) {
          console.log('CatchClause', f);
          console.log('CatchClause', f);
      }
    }
    return (Number(d.replace('.', '')) * Number(e.replace('.', ''))) / Math.pow(10, c);
}
//加法运算中级解决办法
function accAdd(a, b) {  
    var c=0;
    var d=0;
    var e;
    if (a.toString().split('.').length>1) {
      try {
          c = a.toString().split('.')[1].length;
      } catch (f) {
          console.log('CatchClause', f);
          console.log('CatchClause', f);
          c = 0;
      }
    }
    if (b.toString().split('.').length>1) {
      try {
          d = b.toString().split('.')[1].length;
      } catch (f) {
          console.log('CatchClause', f);
          console.log('CatchClause', f);
          d = 0;
      }
    }
    e = Math.pow(10, Math.max(c, d));
    return (a * e + b * e) / e;
}
//返回值：arg1减上arg2的精确结果
function accSub(a, b) {
    var c=0;
    var d=0;
    var e;
    if (a.toString().split('.').length>1) {
      try {
          c = a.toString().split('.')[1].length;
      } catch (f) {
          console.log('CatchClause', f);
          console.log('CatchClause', f);
          c = 0;
      }
    }
    if (b.toString().split('.').length>1) {
      try {
          d = b.toString().split('.')[1].length;
      } catch (f) {
          console.log('CatchClause', f);
          console.log('CatchClause', f);
          d = 0;
      }
    }
    e = Math.pow(10, Math.max(c, d));
    return (a * e - b * e) / e;
}
//返回值：arg1除以arg2的精确结果
function accDiv(a, b) {
    var c;
    var d;
    var e = 0;
    var f = 0;
    if (a.toString().split('.').length>1) {
      try {
          e = a.toString().split('.')[1].length;
      } catch (g) {
          console.log('CatchClause', g);
          console.log('CatchClause', g);
      }
    }
    if (b.toString().split('.').length>1) {
      try {
          f = b.toString().split('.')[1].length;
      } catch (g) {
          console.log('CatchClause', g);
          console.log('CatchClause', g);
      }
    }
    c = Number(a.toString().replace('.', ''));
    d = Number(b.toString().replace('.', ''));
    return (c / d) * Math.pow(10, f - e);
}
function getParamQueryString (url, name) {
  let subStr = name + '='
  if (url.indexOf(subStr) !== -1) {
    let arr = url.split(subStr)
    let sub1 = arr[1]
    if (sub1.indexOf('&') !== -1) {
    let arr2 = sub1.split('&')
    let sub2 = arr2[0]
      return sub2
    } else {
      return sub1
    }
  }
  return ''
}
function checkByRules(formData, rules) {
  var formDataKeys = Object.keys(formData)
  for (var i = 0; i < formDataKeys.length; i++) {
    var key = formDataKeys[i]
    if (rules[key]) {
      var itemRules = rules[key].rules
      var itemValue = formData[key]
      for (var j = 0; j < itemRules.length; j++) {
        var rule = itemRules[j]
        switch (rule.checkType){
          case 'required'://必填项
            if(!itemValue) {return rule.errorMsg;}
            break;
          case 'phone'://手机号码验证
            var reg = /^1[0-9]{10,10}$/;
            if (!reg.test(itemValue)) {return rule.errorMsg;}
            break;
          case 'string'://字符串 checkRule必填
            if(rule.checkRule!=undefined){
              var reg = new RegExp('^.{' + rule.checkRule + '}$');
              if (!reg.test(itemValue)) {return rule.errorMsg;}
              break;
            }else {
              return '';
            }
        }
      }
    }
  }
  return '';
}
module.exports = {
    formatTime: formatTime,
    formatDate: formatDate,
    accMul: accMul,
    accAdd: accAdd,
    accDiv: accDiv,
    accSub: accSub,
    getParamQueryString: getParamQueryString,
    checkByRules: checkByRules
};
